<?
/*
 * Copyright 2004-2005 Sigve Indregard.
 *
 * This file is part of Laivsys.
 *
 * Laivsys is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Laivsys is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Laivsys; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * $Id: stikkord.php 6 2005-07-11 23:14:47Z say $
 */
 
include_once("../db.php");
include_once("auth.php");
include_once("top.php");
include_once("rollerinclude.php");

include("../design.php");

echo "<h1>Stikkord p� nettsiden</h1>\n";

$proc = $_REQUEST['process'];

if ($proc == 'add') {
	$sql = "SELECT Posisjon FROM Stikkord WHERE LaivID=$laivid ORDER BY Posisjon DESC LIMIT 1";
	$maxene = mysql_query($sql);

	if (mysql_num_rows($maxene)) {
		$maxen = mysql_fetch_row($maxene);
		$max = $maxen[0];
	} else {
		$max = 1;
	}

	$tittel = mysql_escape_string($_REQUEST['tittel']);
	$tekst = mysql_escape_string($_REQUEST['tekst']);

	$sql = "INSERT INTO Stikkord (Tittel, Tekst, Posisjon, LaivID) VALUES ('$tittel', '$tekst', $max, $laivid)";
	mysql_query($sql);
	printCurrent();
} elseif ($proc == 'update') {
	$id = mysql_escape_string($_REQUEST['id']);
	$tittel = mysql_escape_string($_REQUEST['tittel']);
	$tekst = mysql_escape_string($_REQUEST['tekst']);

	$sql = "UPDATE Stikkord SET Tittel='$tittel', Tekst='$tekst' WHERE ID=$id";
	mysql_query($sql);
	printCurrent();
} elseif ($proc == 'edit') {
	$id = mysql_escape_string($_REQUEST['id']);
	$records = mysql_query("SELECT * FROM Stikkord WHERE ID=$id");

	if (mysql_num_rows($records)) {
		$record=mysql_fetch_assoc($records);
		$tittel = $record['Tittel'];
		$tekst = $record['Tekst'];

		printEditForm($id, $tittel, $tekst);
	}	
} elseif ($proc == 'moveup') {
	$id = mysql_escape_string($_REQUEST['id']);
	moveit($id, -1);
	printCurrent();
} elseif ($proc == 'movedown') {
	$id = mysql_escape_string($_REQUEST['id']);
	moveit($id, 1);
	printCurrent();
} elseif ($proc == 'delete') {
	$id = mysql_escape_string($_REQUEST['id']);
	$sql = "DELETE FROM Stikkord WHERE ID=$id";
	mysql_query($sql);

	reassignPosisjon();
} else {
	printCurrent();
}

function reassignPosisjon() {
	global $laivid;
	$curpos=1;
	$sql = "SELECT * FROM Stikkord WHERE LaivID=$laivid ORDER BY Posisjon";
	$ordene=mysql_query($sql);
	while ($ord = mysql_fetch_assoc($ordene)) {
		$id = $ord['ID'];
		$sql = "UPDATE Stikkord SET Posisjon=$curpos WHERE ID=$id";
		mysql_query($sql);
		$curpos++;
	}
	
}

function moveit ($id, $change) {
	global $laivid;
	reassignPosisjon(); //ensure they are in order

	$sidden=mysql_query("SELECT Posisjon FROM Stikkord WHERE ID=$id");
	$sid=mysql_fetch_assoc($sidden) or die(mysql_error());

	$maxene=mysql_query("SELECT * FROM Stikkord");
	$max=mysql_num_rows($maxene);
	
	$curpos = $sid['Posisjon'];
	$newpos = $curpos + $change;

	if ($newpos > 0 && $newpos < ($max + 1)) {
		$sql = "UPDATE Stikkord SET Posisjon=$curpos WHERE Posisjon=$newpos AND LaivID=$laivid";
		mysql_query($sql);
		$sql = "UPDATE Stikkord SET Posisjon=$newpos WHERE ID=$id";
		mysql_query($sql);
	}
}

function printCurrent() {
	global $laivid;
	?>
	<h2>N�v�rende stikkord</h2>
	<table class="gridded">
	<?
	$sql = "SELECT * FROM Stikkord WHERE LaivID=$laivid ORDER BY Posisjon";
	$stikkordene = mysql_query($sql);
	
	$antall_stikkord = mysql_num_rows($stikkordene);
	$i=0;
	
	while ($stikk = mysql_fetch_assoc($stikkordene)) {
		?>
		<tr>
			<th><?=$stikk['Tittel']?></th>
			<td style="text-align: right">
				<a href="stikkord.php?id=<?=$stikk['ID']?>&process=edit">[rediger]</a>
			</td>
			<td>
			<?
			if ($i != 0)
				echo "<a href=\"stikkord.php?id={$stikk['ID']}&process=moveup\">[opp]</a>\n";

			echo "</td><td>";
			
			if ($i != ($antall_stikkord - 1))
				echo "<a href=\"stikkord.php?id={$stikk['ID']}&process=movedown\">[ned]</a>\n";
			?>
			</td>
		</tr>
		<?
		/*
		<tr>
			<td colspan="4"><blockquote><?=txt2html($stikk['Tekst'])?></td>
		</tr>
		*/
		$i++;
	}
	echo "</table>\n";
	printEditForm(0, "", "");
}

function printEditForm($id, $tittel, $tekst) {
	if ($id == 0) {
		$action = "Legg til";
		$process="add";
	} else {
		$action = "Endre";
		$process="update";
	}
	
	echo "<h2>$action</h2>\n";
	echo "<form method=\"post\" action=\"stikkord.php\">\n";
	echo "\t<input type=\"hidden\" name=\"process\" value=\"$process\"/>\n";
	echo "\t<input type=\"hidden\" name=\"id\" value=\"$id\"/>\n";
	echo "\t<p><b>Tittel</b><br/>\n";
	echo "\t<input name=\"tittel\" value=\"$tittel\"/></p>\n";
	echo "\t<p><b>Tekst</b><br/>\n";
	echo "\t<textarea name=\"tekst\" cols=\"40\" rows=\"8\">$tekst</textarea></p>\n";
	echo "\t<input type=\"submit\" value=\"Lagre\"/>\n";
	echo "\t<input type=\"reset\" value=\"Tilbakestill\"/>\n";
	echo "</form>\n";
}

include("bottom.php");
?>
